<?php

	class LoginCommand
	{
		public function execute($event)
		{
			$userModel = EbModel::getModel('Community','User');
			$session = EbController::$session;
			$data = $event->data;
			$ref = isset($data['ref']) ? $data['ref'] : '';
			
			if (strlen($ref) == 0)
			{
				$ref = '/';
			}
						
			$login = $data['login'];
			$pwd = $data['password'];
						
			$user = $userModel->selectByLoginAndPassword($login,$pwd);
			
			if (count($user) == 0)
			{
				$session->setFlashMessage('login','Wrong login or password!');
				
				return new Event('redirect','/login');
			}
			else
			{
				$user = $user[0];
			
				$session->setData("login",$user->login);			
				$session->setData("email",$user->email);
				$session->setData("us_id",$user->us_id);
				$session->setData("roles",$user->user_roles);
				
				Community_BasicUser::addRoles(split(',',$user->user_roles));
			
				return new Event('redirect',$ref);
			}
		}	
	}